//
//  JwExService.m
//  JwPack
//
//  Created by 陈警卫 on 2024-09-06.
//

#import "JwExService.h"

@implementation JwExService

- (void)login_view:(UIView *)view userName:(NSString *)userName password:(NSString *)password comple:(void (^)(JwLoginModel *loginModel, JwHttpConfig *config))comple {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userName"] = userName;
    param[@"password"] = password;
    JwHttpConfig *config = [self.httpManager requestHandel:kJwHttpMethodPOST point:@"/doctor/user/login" params:param headers:nil owners:nil decodeModelName:@"JwLoginModel" decodePath:@"data" comple:^(JwHttpConfig * _Nonnull config) {
        if (config.state == JwHttpStateFinish) {
            JwLocalModel *localModel = [[JwLocalModel alloc] init];
            localModel.isLogin = YES;
            localModel.loginModel = (JwLoginModel *)config.decode;
            [JwLocalCenter shared].localBaseModel = localModel;
            [[JwLocalCenter shared] save];
            if (comple) {
                comple(localModel.loginModel, config);
            }
        }
    }];
    config.loadView = view;
}

- (void)refreshTokenTest_scrollView:(UIScrollView *)scrollView comple:(void (^)(JwHttpConfig *config))comple {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"refreshToken"] = [JwLocalCenter shared].loginModel.token.refreshToken;
    JwHttpConfig *config = [self.httpManager requestHandel:kJwHttpMethodPOST point:@"/doctor/user/login/refresh-token" params:param headers:nil owners:nil decodeModelName:@"JwTokenModel" decodePath:@"data" comple:^(JwHttpConfig * _Nonnull config) {
        if (config.state == JwHttpStateFinish) {
            JwTokenModel *tokenModel = (JwTokenModel *)config.decode;
            [JwLocalCenter shared].loginModel.token.refreshToken = tokenModel.refreshToken;
            [[JwLocalCenter shared] save];
            if (comple) {
                comple(config);
            }
        }
    }];
    config.scrollView = scrollView;
}

- (void)userInfo_view:(UIView *)view comple:(void (^)(JwHttpConfig *config))comple {
    JwHttpConfig *config = [self.httpManager requestHandel:kJwHttpMethodGET point:@"/doctor/user/info" params:nil headers:nil owners:nil decodeModelName:@"JwUserModel" decodePath:@"data" comple:^(JwHttpConfig * _Nonnull config) {
        if (config.state == JwHttpStateFinish) {
            JwUserModel *userModel = (JwUserModel *)config.decode;
            [JwLocalCenter shared].loginModel.userInfo = userModel;
            [[JwLocalCenter shared] save];
            if (comple) {
                comple(config);
            }
        }
    }];
    config.loadView = view;
}

- (void)refreshToken_comple:(void (^)(JwHttpConfig *config))comple {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"refreshToken"] = [JwLocalCenter shared].loginModel.token.refreshToken;
    NSMutableDictionary *owners = [NSMutableDictionary dictionary];
    owners[kJwHttpOwnerNoLockKey] = @"1";
    [self.httpManager requestHandel:kJwHttpMethodPOST point:@"/doctor/user/login/refresh-token" params:param headers:nil owners:owners decodeModelName:@"JwTokenModel" decodePath:@"data" comple:^(JwHttpConfig * _Nonnull config) {
        if (config.state == JwHttpStateFinish) {
            JwTokenModel *tokenModel = (JwTokenModel *)config.decode;
            [JwLocalCenter shared].loginModel.token = tokenModel;
            [[JwLocalCenter shared] save];
            if (comple) {
                comple(config);
            }
        }
    }];
}

- (void)refresh_config:(JwHttpConfig *)config comple:(void (^)(JwHttpConfig *config))comple {
    NSMutableDictionary *owners = [NSMutableDictionary dictionary];
    owners[kJwHttpOwnerRefreshHeadersKey] = @"1";
    owners[kJwHttpOwnerNoLockKey] = @"0";
    [config.owners addEntriesFromDictionary:owners];
    [self.httpManager requestHandelWithConfig:config comple:comple];
}


@end
